package com.zhonghui.modlules.user.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.eshop.apiCode.EshopException;
import com.zhonghui.common.bean.LocalUser;
import com.zhonghui.common.eshopMapper.SystemCityMapper;
import com.zhonghui.common.eshopUtil.ApiResult;
import com.zhonghui.common.eshopVo.CityVo;
import com.zhonghui.common.util.CityTreeUtil;
import com.zhonghui.modlules.user.eshopDomain.SystemCity;
import com.zhonghui.modlules.user.eshopParam.AddressParam;
import com.zhonghui.modlules.user.eshopService.SystemCityService;
import com.zhonghui.modlules.user.eshopService.UserAddressService;
import com.zhonghui.modlules.user.eshopVo.UserAddressQueryVo;
import com.zhonghui.modlules.user.param.IdParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(value = "用户地址", tags = "用户:用户地址", description = "用户地址")
public class UserAddressController {

    private final UserAddressService userAddressService;
    private final SystemCityMapper systemCityMapper;
    private final SystemCityService systemCityService;


    @ApiOperation(value = "城市列表",notes = "城市列表")
    @GetMapping("/city/list")
    public ApiResult<List<CityVo>> cityList(){
        List<SystemCity> systemCities = systemCityService.list();
        ArrayList<CityVo> cityVos = Lists.newArrayList();
        for (SystemCity systemCity : systemCities) {
            CityVo cityVo = new CityVo();
            cityVo.setValue(systemCity.getCityId());
            cityVo.setLabel(systemCity.getName());
            cityVo.setPid(systemCity.getParentId());

            cityVos.add(cityVo);
        }

        return ApiResult.ok(CityTreeUtil.list2TreeConverter(cityVos,0));
    }

    @GetMapping("/city_list/{cityId}")
    public ApiResult<Object> city(@PathVariable String cityId){
        LambdaQueryWrapper<SystemCity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(cityId)){
            queryWrapper.eq(SystemCity::getCityId,cityId);
        }
        List<SystemCity> systemCities = systemCityMapper.selectList(queryWrapper);
        return ApiResult.ok(systemCities);
    }

    @ApiOperation(value = "用户地址列表",notes = "用户地址列表")
    @GetMapping("/address/user")
    public ApiResult<List<UserAddressQueryVo>> addressUser(@RequestParam(value = "page",defaultValue = "1")int page,
                                                           @RequestParam(value = "limit",defaultValue = "10")int limit){
        Long uid = LocalUser.getUser().getUid();
        List<UserAddressQueryVo> userAddressQueryVos = userAddressService.getList(uid, page, limit);
        return ApiResult.ok(userAddressQueryVos);
    }

    @ApiOperation(value = "添加或修改地址",notes = "添加或修改地址")
    @PostMapping("/address/edit")
    public ApiResult<Map<String,Object>> addressEdit(@Validated @RequestBody AddressParam addressParam){
        Long uid = LocalUser.getUser().getUid();
        Map<String, Object> map = new LinkedHashMap<>();
        Long id = userAddressService.addAndEdit(uid, addressParam);
        map.put("id",id);
        return ApiResult.ok(map);
    }

    @ApiOperation(value = "删除地址",notes = "删除地址")
    @PostMapping("/address/dels")
    public ApiResult<Boolean> addressDel(@Validated @RequestBody IdParam idParam){
        userAddressService.removeById(idParam.getId());
        return ApiResult.ok();
    }

    @ApiOperation(value = "设置默认地址",notes = "设置默认地址")
    @PostMapping("/address/default")
    public ApiResult<Boolean> addressDefault(@Validated @RequestBody IdParam idParam){
        Long uid = LocalUser.getUser().getUid();
        userAddressService.setDefault(uid,Long.valueOf(idParam.getId()));
        return ApiResult.ok();
    }

    @ApiOperation(value = "地址详情",notes = "地址详情")
    @GetMapping("/address/detail/{id}")
    public ApiResult<UserAddressQueryVo> addressDetail(@PathVariable String id){
        if (StrUtil.isBlank(id) || !NumberUtil.isNumber(id)){
            throw new EshopException("参数错误");
        }
        return ApiResult.ok(userAddressService.getDetail(Long.valueOf(id)));
    }
}
